package com.changgou.goods.service.impl;

import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;


    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }


    //id查询品牌
    @Override
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    //增加数据
    @Override
    public void save(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    //修改数据
    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }


    //根据id删除数据
    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }


    //根据条件查询
    @Override
    public List<Brand> findList(Brand brand) {

        //Exampl 为条件查询类,里面有很多条件语句
        Example example = createExample(brand);

        //构建查询条件

        List<Brand> brandList = brandMapper.selectByExample(example);

        //返回结果

        return brandList;
    }

    @Override
    public PageInfo<Brand> findPage(Integer page, Integer size) {

        //创建分页
        //page :第几页的页数
        //size :每页显示数量
        PageHelper.startPage(page,size );

        //查询分页  //第一个查询才是分页

        List<Brand> brandList = brandMapper.selectAll();  //查询一

        List<Brand> brandList2 = brandMapper.selectAll();  //查询二


        //查看结果
        //验证第一个才有分页的效果10页,第二个是显示总数
        //10:::175
        System.out.println(brandList.size()+":::"+brandList2.size());
        //封装结果集

        return new PageInfo<Brand>(brandList);   //Pageinfo会自动解析brandList分页
    }


    //分页条件查询
    @Override
    public PageInfo<Brand> findPage(Integer page, Integer size, Brand brand) {

        //创建分页

        PageHelper.startPage(page,size );

        //拼接条件
        Example example = createExample(brand);

        //查询结果
        List<Brand> brandList = brandMapper.selectByExample(example);

        //返回结果
        return new PageInfo<Brand>(brandList);
    }



    //条件查询提取模块
    private Example createExample(Brand brand) {  //ctrl + alt + M 提取公用模块
        //创建条件查询
        //select * from tb_brand
        Example example = new Example(Brand.class);  //创建查询条件,标识为那个表

        Example.Criteria criteria = example.createCriteria();

        //拼接条件

        if (brand!=null){

            if (!StringUtils.isEmpty(brand.getName())){  //判断条件不为空是就拼接

                // where name like %brand.getName()%
                //第一个参数为: 需要拼接的字段
                //第二个参数为: 拼接的条件
                criteria.andLike("name","%"+brand.getName()+"%" );
            }
            if (!StringUtils.isEmpty(brand.getLetter())){  //判断条件不为空是就拼接

                // where name like %brand.getLetter()%
                //第一个参数为: 需要拼接的字段
                //第二个参数为: 拼接的条件
                criteria.andLike("name","%"+brand.getLetter()+"%" );
            }

        }
        return example;
    }
}
